package com.sweets.look.ui.setting;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sweet.satellite.GlobalConfig;
import com.sweet.satellite.TranslucentSBActivity;
import com.sweets.look.R;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.widget.MarkRadioDialog;
import com.sweets.look.util.ViewUtils;

public class OperationSettings extends TranslucentSBActivity {
    private CheckBox mCbVolume;
    private Context mContext;
    private TextView mGestureToolbarLeft;
    private TextView mGestureToolbarRight;
    private TextView mKeyBackText;
    private TextView mKeyForwardText;
    private TextView mKeyHomeText;
    private TextView mKeyMenuText;
    private TextView mKeyTabText;
    private PreferenceManager mPreferences;


    private void initTextView() {
        this.mPreferences = PreferenceManager.getInstance();
        this.mKeyForwardText = (TextView) this.findViewById(R.id.keyForwardText);
        this.setTextView(this.mKeyForwardText, this.mPreferences.getKeyForwardChoice());
        this.mKeyBackText = (TextView) this.findViewById(R.id.keyBackText);
        this.setTextView(this.mKeyBackText, this.mPreferences.getKeyBackChoice());
        this.mKeyHomeText = (TextView) this.findViewById(R.id.keyHomeText);
        this.setTextView(this.mKeyHomeText, this.mPreferences.getKeyHomeChoice());
        this.mKeyTabText = (TextView) this.findViewById(R.id.keyTabText);
        this.setTextView(this.mKeyTabText, this.mPreferences.getKeyTabChoice());
        this.mKeyMenuText = (TextView) this.findViewById(R.id.keyMenuText);
        this.setTextView(this.mKeyMenuText, this.mPreferences.getKeyMenuChoice());
        this.mGestureToolbarLeft = (TextView) this.findViewById(R.id.gestureToolbarLeft);
        this.setTextView(this.mGestureToolbarLeft, this.mPreferences.getGestureToolbarLeftChoice());
        this.mGestureToolbarRight = (TextView) this.findViewById(R.id.gestureToolbarRight);
        this.setTextView(this.mGestureToolbarRight, this.mPreferences.getGestureToolbarRightChoice());
    }

    private void initVolumePreference() {
        this.mCbVolume = (CheckBox) this.findViewById(R.id.cbVolume);
        this.mCbVolume.setChecked(this.mPreferences.getVolumeScroll());
        this.mCbVolume.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setVolumeScrollEnabled(isChecked);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.settings_operation);
        this.setToolbarTitle(R.string.settings_operation);

        this.mContext = this;
        this.initTextView();
        this.initVolumePreference();
        ViewUtils.swipeToFinish(this.findViewById(R.id.settingLayout), this);
    }

    public void onSettingsItemClick(View v) {
        boolean v0 = true;
        switch (v.getId()) {
            case R.id.layoutKeyBack: {
                this.showSettingDialog(0, this.mPreferences.getKeyBackChoice());
                break;
            }
            case R.id.layoutKeyForward: {
                this.showSettingDialog(1, this.mPreferences.getKeyForwardChoice());
                break;
            }
            case R.id.layoutKeyHome: {
                this.showSettingDialog(2, this.mPreferences.getKeyHomeChoice());
                break;
            }
            case R.id.layoutKeyTab: {
                this.showSettingDialog(3, this.mPreferences.getKeyTabChoice());
                break;
            }
            case R.id.layoutKeyMenu: {
                this.showSettingDialog(4, this.mPreferences.getKeyMenuChoice());
                break;
            }
            case R.id.layoutGestureToolbarLeft: {
                this.showSettingDialog(5, this.mPreferences.getGestureToolbarLeftChoice());
                break;
            }
            case R.id.layoutGestureToolbarRight: {
                this.showSettingDialog(6, this.mPreferences.getGestureToolbarRightChoice());
                break;
            }
            case R.id.layoutVolume: {

                if (this.mCbVolume.isChecked()) {
                    v0 = false;
                }
                mCbVolume.setChecked(v0);
                break;
            }
        }
    }


    private void setTextView(TextView tv, int choice) {
        switch (choice) {
            case 0: {
                tv.setText(this.getResources().getString(R.string.operation_none));
                break;
            }
            case 1: {
                tv.setText(this.getResources().getString(R.string.operation_reload));
                break;
            }
            case 2: {
                tv.setText(this.getResources().getString(R.string.operation_top));
                break;
            }
            case 3: {
                tv.setText(this.getResources().getString(R.string.operation_bottom));
                break;
            }
            case 4: {
                tv.setText(this.getResources().getString(R.string.operation_input));
                break;
            }
            case 5: {
                tv.setText(this.getResources().getString(R.string.operation_newtab));
                break;
            }
            case 6: {
                tv.setText(this.getResources().getString(R.string.operation_addbookmark));
                break;
            }
            case 7: {
                tv.setText(this.getResources().getString(R.string.operation_openbookmark));
                break;
            }
            case 8: {
                tv.setText(this.getResources().getString(R.string.operation_openhistory));
                break;
            }
            case 9: {
                tv.setText(this.getResources().getString(R.string.operation_closetab));
                break;
            }
            case 10: {
                tv.setText(this.getResources().getString(R.string.operation_lasttab));
                break;
            }
            case 11: {
                tv.setText(this.getResources().getString(R.string.operation_nexttab));
                break;
            }
            case 12: {
                tv.setText(this.getResources().getString(R.string.operation_goback));
                break;
            }
            case 13: {
                tv.setText(this.getResources().getString(R.string.operation_goforward));
                break;
            }
            case R.id.layoutVolume: {
                CheckBox v1 = this.mCbVolume;
                boolean v0 = !this.mCbVolume.isChecked();
                v1.setChecked(v0);
                break;
            }
        }
    }

    private void showSettingDialog(final int k, int i) {
        new MarkRadioDialog(this.mContext).builder()
                .setTitle(this.getResources().getString(R.string.settings_operation))
                .setCancelable(true)
                .setSingleChoiceItems(R.array.array_operation, i, new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (k) {
                            case 0: {
                                mPreferences.setKeyBackChoice(checkedId);
                                setTextView(mKeyBackText, checkedId);
                                break;
                            }
                            case 1: {
                                mPreferences.setKeyForwardChoice(checkedId);
                                setTextView(mKeyForwardText, checkedId);
                                break;
                            }
                            case 2: {
                                mPreferences.setKeyHomeChoice(checkedId);
                                setTextView(mKeyHomeText, checkedId);
                                break;
                            }
                            case 3: {
                                mPreferences.setKeyTabChoice(checkedId);
                                setTextView(mKeyTabText, checkedId);
                                break;
                            }
                            case 4: {
                                mPreferences.setKeyMenuChoice(checkedId);
                                setTextView(mKeyMenuText, checkedId);
                                break;
                            }
                            case 5: {
                                mPreferences.setGestureToolbarLeftChoice(checkedId);
                                setTextView(mGestureToolbarLeft,
                                        checkedId);
                                break;
                            }
                            case 6: {
                                mPreferences.setGestureToolbarRightChoice(checkedId);
                                setTextView(mGestureToolbarRight,
                                        checkedId);
                                break;
                            }
                        }
                    }
                }).show();
    }
}

